package com.example.demo.controllers;

import com.example.demo.form.*;
import com.example.demo.service.IUserService;
import com.example.demo.vo.Result;
import com.example.demo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author HL
 */
@RestController
@CrossOrigin(origins = "*")

public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping(value = "/user/check")
    public Result check(@RequestParam("EMAIL") String email) {
        String answer = userService.sendEmail(email);
        if(!Objects.equals(answer, "error")) {
            return Result.success(answer);
        }else {
            return Result.fail();
        }
    }

    @GetMapping(value = "/user/login/auto")
    public Result autoLogin(@RequestParam("CIPHERTEXT") String cipherText) {
        UserVo user = userService.logonAuto(cipherText);
        if(user != null) {
            return Result.success(user);
        }else {
            return new Result(10008,"密文无效");
        }
    }

    @PostMapping(value = "/user/login/account")
    public Result loginByAccount(@RequestBody AccountAndPasswordForm accountAndPasswordForm) {
        UserVo userVo = userService.logonByAccount(accountAndPasswordForm.getUserAccount(), accountAndPasswordForm.getUserPassword());
        if(userVo.getAccount().compareTo("10003") == 0) {
            return new Result(10003,"账号或密码错误");
        }else if(userVo.getAccount().compareTo("10004") == 0) {
            return new Result(10004,"账号不存在");
        }else {
            return Result.success(userVo);
        }
    }

    @PostMapping(value = "/user/login/email")
    public Result loginByEmail(@RequestBody EmailAndCheckForm emailAndCheckForm) {
        UserVo userVo = userService.logonByEmail(emailAndCheckForm.getEmail(), emailAndCheckForm.getCheck());
        if(userVo.getAccount().compareTo("10004") == 0) {
            return new Result(10004,"验证码错误或过期");
        }else if(userVo.getAccount().compareTo("10006") == 0) {
            return new Result(10006,"该邮箱未注册");
        }else {
            return Result.success(userVo);
        }
    }

    @PostMapping(value = "/user/signup")
    public Result register(@RequestBody NamePwdEmailAndCheckForm namePwdEmailAndCheckForm) {
        UserVo userVo = userService.register(namePwdEmailAndCheckForm.getUserName(), namePwdEmailAndCheckForm.getUserPassword(), namePwdEmailAndCheckForm.getEmail(), namePwdEmailAndCheckForm.getCheck());
        if(userVo.getAccount().compareTo("10004") == 0) {
            return new Result(10004, "验证码错误或过期");
        }else if(userVo.getAccount().compareTo("10005") == 0) {
            return new Result(10005, "该邮箱已被注册");
        }
        return Result.success(userVo);
    }

    @PostMapping(value = "/user/update/user")
    public Result updateUser(@RequestBody AccountNameAvatarForm accountNameAvatarForm) {
        userService.updateName(accountNameAvatarForm.getUserAccount(), accountNameAvatarForm.getUserName());
        UserVo userVo = userService.updateAvatar(accountNameAvatarForm.getUserAccount(), accountNameAvatarForm.getUserAvatar());
        return Result.success(userVo);
    }

    @PostMapping(value = "/user/update/password")
    public Result updatepwdByAccount(@RequestBody AccountPwdAndNewpwdForm accountPwdAndNewpwd) {
        UserVo userVo = userService.logonByAccount(accountPwdAndNewpwd.getUserAccount(), accountPwdAndNewpwd.getUserPassword());
        if(userVo.getAccount().compareTo("10003") == 0) {
            return new Result(10003,"账号或密码错误");
        }else if(userVo.getAccount().compareTo("10004") == 0) {
            return new Result(10004,"账号不存在");
        }else {
            userService.updatePwd(accountPwdAndNewpwd.getUserAccount(), accountPwdAndNewpwd.getUserNewPassword());
            return Result.success(userVo);
        }
    }

    @PostMapping(value = "/user/update/email")
    public Result updatepwdByEmail(@RequestBody EmailCheckAndNewpwdForm emailCheckAndNewpwd) {
        UserVo userVo = userService.logonByEmail(emailCheckAndNewpwd.getEmail(), emailCheckAndNewpwd.getCheck());
        if(userVo.getAccount().compareTo("10004") == 0) {
            return new Result(10004,"验证码错误或过期");
        }else if(userVo.getAccount().compareTo("10006") == 0) {
            return new Result(10006,"该邮箱未注册");
        }
        userService.updatePwd(userVo.getAccount(), emailCheckAndNewpwd.getUserNewPassword());
        return Result.success(userVo);
    }

    @DeleteMapping(value = "/user/delete")
    public Result cancel(@RequestBody AccountAndPasswordForm accountAndPasswordForm) {
        UserVo userVo = userService.logonByAccount(accountAndPasswordForm.getUserAccount(), accountAndPasswordForm.getUserPassword());
        if(userVo.getAccount().compareTo("10003") == 0) {
            return new Result(10003,"账号或密码错误");
        }else if(userVo.getAccount().compareTo("10004") == 0) {
            return new Result(10004,"账号不存在");
        }
        userService.deleteUserByAccount(accountAndPasswordForm.getUserAccount());
        return Result.success();
    }
}
